package com.zp.demopractice1;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zp.demopractice1.Utils.UIUtils;
import com.zp.demopractice1.me.PagerIndicatorView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private int[] imgs = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d};
    private List<View> viewList;
    CarouselView bannerView;
    ViewPager viewPager;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    startActivity(new Intent(MainActivity.this,MZBannerActivity.class));
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(this);
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            viewList.add(image);
        }
        bannerView = (CarouselView) findViewById(R.id.banner);
        bannerView.setViewList(viewList);
        bannerView.startLoop(true);
        ImageView imageView = (ImageView) findViewById(R.id.iv);
        ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
//        shapeDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        shapeDrawable.getPaint().setColor(ContextCompat.getColor(this,R.color.colorAccent));
        shapeDrawable.setIntrinsicHeight(UIUtils.dpToPx(20));
        shapeDrawable.setIntrinsicWidth(UIUtils.dpToPx(20));
        imageView.setImageDrawable(shapeDrawable);
//        shapeDrawable.setBounds(UIUtils.dpToPx(2),UIUtils.dpToPx(2), UIUtils.dpToPx(20),UIUtils.dpToPx(20));
//        imageView.setBackgroundDrawable(shapeDrawable);

//        bannerView.setTransformAnim(true);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }
        });
        PagerIndicatorView pagerIndicatorView = (PagerIndicatorView) findViewById(R.id.indicator);
        pagerIndicatorView.bindWithViewPager(viewPager,viewList.size());
    }
    private static class MainBrodcastReceiver extends BroadcastReceiver{
        private WeakReference<MainActivity> wsActivity = null;
        public MainBrodcastReceiver(MainActivity activity) {
            wsActivity = new WeakReference<MainActivity>(activity);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (null!=wsActivity.get()){
                wsActivity.get().unpdateList();
            }
        }
    }

    private void unpdateList(){

    }

}
